package com.yic.module.base.controller.admin.center;

import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.common.enums.CommonStatusEnum;
import com.yic.framework.common.util.collection.CollectionUtils;
import com.yic.module.base.controller.admin.factory.vo.FactorySimpleRespVO;
import com.yic.module.base.convert.factory.FactoryConvert;
import com.yic.module.base.dal.dataobject.factory.FactoryDO;
import com.yic.module.base.service.factory.FactoryService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.base.controller.admin.center.vo.*;
import com.yic.module.base.dal.dataobject.center.CenterDO;
import com.yic.module.base.convert.center.CenterConvert;
import com.yic.module.base.service.center.CenterService;

@Tag(name = "管理后台 - 工作中心")
@RestController
@RequestMapping("/base/center")
@Validated
public class CenterController {

    @Resource
    private CenterService centerService;
    @Resource
    private FactoryService factoryService;

    @PostMapping("/create")
    @Operation(summary = "创建工作中心")
    @PreAuthorize("@ss.hasPermission('base:center:create')")
    public CommonResult<Long> createCenter(@Valid @RequestBody CenterCreateReqVO createReqVO) {
        return success(centerService.createCenter(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工作中心")
    @PreAuthorize("@ss.hasPermission('base:center:update')")
    public CommonResult<Boolean> updateCenter(@Valid @RequestBody CenterUpdateReqVO updateReqVO) {
        centerService.updateCenter(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工作中心")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:center:delete')")
    public CommonResult<Boolean> deleteCenter(@RequestParam("id") Long id) {
        centerService.deleteCenter(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工作中心")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:center:query')")
    public CommonResult<CenterRespVO> getCenter(@RequestParam("id") Long id) {
        CenterDO center = centerService.getCenter(id);
        return success(CenterConvert.INSTANCE.convert(center));
    }

    @GetMapping("/list")
    @Operation(summary = "获得工作中心列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('base:center:query')")
    public CommonResult<List<CenterRespVO>> getCenterList(@RequestParam("ids") Collection<Long> ids) {
        List<CenterDO> list = centerService.getCenterList(ids);
        return success(CenterConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工作中心分页")
    @PreAuthorize("@ss.hasPermission('base:center:query')")
    public CommonResult<PageResult<CenterRespVO>> getCenterPage(@Valid CenterPageReqVO pageVO) {
        PageResult<CenterDO> pageResult = centerService.getCenterPage(pageVO);
        List<CenterDO> list = pageResult.getList();
        // 获取拼接所需要的数据
        List<Long> factoryIds = CollectionUtils.convertList(list, CenterDO::getFactoryId);
        Map<Long, FactoryDO> factoryDOMap = factoryService.getFactoryMap(factoryIds);
        // 拼接结果返回
        List<CenterRespVO> centerList = new ArrayList<>(list.size());
        list.forEach(centerDO -> {
            CenterRespVO respVO = CenterConvert.INSTANCE.convert(centerDO);
            FactoryDO factory = factoryDOMap.get(centerDO.getFactoryId());
            if (ObjectUtil.isNotEmpty(factory)){
                respVO.setFactoryName(factory.getName());
            }
            centerList.add(respVO);
        });
        return success(new PageResult<>(centerList, pageResult.getTotal()));
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取工作中心精简信息列表", description = "只包含被开启的工作中心，主要用于前端的下拉选项")
    public CommonResult<List<CenterSimpleRespVO>> getSimpleCenterList() {
        // 获得工作中心列表，只要开启状态的
        List<CenterDO> list = centerService.getCenterList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
        // 排序后，返回给前端
        list.sort(Comparator.comparing(CenterDO::getSort));
        return success(CenterConvert.INSTANCE.convertList03(list));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工作中心 Excel")
    @PreAuthorize("@ss.hasPermission('base:center:export')")
    @OperateLog(type = EXPORT)
    public void exportCenterExcel(@Valid CenterExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CenterDO> list = centerService.getCenterList(exportReqVO);
        // 导出 Excel
        List<CenterExcelVO> datas = CenterConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "工作中心.xls", "数据", CenterExcelVO.class, datas);
    }

}
